import 'package:boss/models/commodity.dart';
import 'package:boss/provider/offers_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/doubleOffers/provider/double_offers_provider.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/orderDiscount/widget/offer_commodity_item.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/widget/offers_form_field_common.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/custom_divier.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/picker/commodityPicker/commodity_picker.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class DoubleOffersBrowser extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.symmetric(
              vertical: 30.w,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                OffersFormFieldCommon<DoubleOffersProvider>(
                  title: '第二份优惠',
                ),
                CustomDivider(
                  height: 20.w,
                ),
                Padding(
                  padding: EdgeInsets.symmetric(
                    horizontal: 55.w,
                  ),
                  child: Column(
                    children: <Widget>[
                      Consumer<DoubleOffersProvider>(
                        builder: (context, doubleOffersProvider, child) {
                          return Column(
                            children: <Widget>[
                              SelectFormField<int>(
                                title: '第二份优惠方式',
                                items: doubleOffersProvider.secondTypes,
                                value: doubleOffersProvider.secondType,
                                onChanged: (value) {
                                  doubleOffersProvider.setSecondType(value);
                                },
                              ),
                              doubleOffersProvider.secondType == 0
                                  ? BaseTextFormField(
                                      key: Key('rate_field'),
                                      prefixText: '第二份折扣率',
                                      hintText: '例：打九折，填写90',
                                      suffixText: '%',
                                      maxLength: 2,
                                      keyboardType: TextInputType.number,
                                      inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
                                      onChanged: (value) {
                                        doubleOffersProvider.setDiscountRate(value);
                                      },
                                      initialValue: doubleOffersProvider.discountRate,
                                    )
                                  : BaseTextFormField(
                                      key: Key('less_field'),
                                      prefixText: '减免金额',
                                      hintText: '请输入减免金额',
                                      suffixText: '元',
                                      keyboardType: TextInputType.numberWithOptions(signed: true),
                                      inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
                                      onChanged: (value) {
                                        doubleOffersProvider.setLessPrice(value);
                                      },
                                      initialValue: doubleOffersProvider.lessPrice,
                                    ),
                            ],
                          );
                        },
                      ),
                      CustomListTitle(
                        title: Consumer<DoubleOffersProvider>(
                          builder: (context, doubleOffersProvider, _) {
                            return RichText(
                              text: TextSpan(text: '活动菜品', style: TextStyleConstant().normal_1_28, children: [
                                TextSpan(
                                  text: '（${doubleOffersProvider.commoditys.length}）',
                                  style: TextStyleConstant().origin_24,
                                ),
                              ]),
                            );
                          },
                        ),
                        isHideDivider: true,
                        trailing: LabelButton(
                          title: '+选择菜品',
                          textStyle: TextStyleConstant().blue_24,
                          onTap: () async {
                            var doubleOffersProvider = Provider.of<DoubleOffersProvider>(
                              context,
                              listen: false,
                            );
                            var result = await showCommodityPicker(
                              context,
                              selectedCommodities: doubleOffersProvider.commoditys,
                              isShowSku: true,
                              isShowPackage: false,
                              filterType: [Commodity.COMMODITY_TYPE_WEIGH, Commodity.COMMODITY_TYPE_PACKAGE],
                            );
                            if (result != null) {
                              doubleOffersProvider.setCommoditys(result);
                            }
                          },
                        ),
                      ),
                      Consumer<DoubleOffersProvider>(
                        builder: (context, doubleOffersProvider, child) {
                          return ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemBuilder: (context, index) {
                              var current = doubleOffersProvider.commoditys[index];
                              return OfferCommodityItem(
                                data: current,
                                onDeleted: () {
                                  doubleOffersProvider.removeCommodity(current);
                                },
                              );
                            },
                            itemCount: doubleOffersProvider.commoditys.length,
                          );
                        },
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
        FixedBottomBtn(
          title: '保 存',
          onTap: () async {
            var doubleOffersProvider = Provider.of<DoubleOffersProvider>(
              context,
              listen: false,
            );
            var offersProvider = Provider.of<OffersProvider>(
              context,
              listen: false,
            );

            var flag = await doubleOffersProvider.addDoubleOffers();
            if (flag) {
              offersProvider.backstageRefresh();

              Navigator.pop(context);
            }
          },
        ),
      ],
    );
  }
}
